﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using TechCraftEngine.Common;
using TechCraftEngine.WorldEngine;
using System.Diagnostics;

namespace TechCraftEngine.Components
{
    public class BlockPicker : TechEngineComponent
    {
        private Texture2D _tileSelectorTexture;
        private Texture2D _tileStripTexture;
        private BlockType _blockType = BlockType.Brick;
        private SpriteBatch _spiteBatch;
        private float _drawX;
        private float _drawY;
        private MouseState _previousMouseState;

        public BlockPicker(TechCraftGame game, SpriteBatch spriteBatch)
            : base(game)
        {
            _spiteBatch = spriteBatch;
        }

        private Texture2D _textureAtlas;

        public BlockType SelectedBlockType
        {
            get { return _blockType; }
        }

        public override void Initialize()
        {
            _textureAtlas = Game.Content.Load<Texture2D>("Textures\\blocks");

            Blocks.Add((BlockType.Brick));
            Blocks.Add((BlockType.Dirt));
            Blocks.Add((BlockType.Gold));

            Blocks.Add((BlockType.Grass));
            Blocks.Add((BlockType.Iron));
            Blocks.Add((BlockType.Lava));

            Blocks.Add((BlockType.Leaves));
            Blocks.Add((BlockType.RedFlower));
            Blocks.Add((BlockType.Rock));

            Blocks.Add((BlockType.Ladder));
            Blocks.Add((BlockType.TNT));
            Blocks.Add((BlockType.Tree));

            Blocks.Add((BlockType.Water));
        }

        public override void LoadContent()
        {
            _tileSelectorTexture = Game.Content.Load<Texture2D>("Textures\\tileSelector");
            _tileStripTexture = Game.Content.Load<Texture2D>("Textures\\tileStrip");

            _drawX = (Game.GraphicsDevice.Viewport.Width / 2) - (_tileStripTexture.Width / 2);
            _drawY = Game.GraphicsDevice.Viewport.Height - 70;

            _drawX = _drawX + 32;
        }

        public Texture2D GetSideTexture(BlockType blck)
        {
            Color[] imageData = new Color[_textureAtlas.Width * _textureAtlas.Height];
            _textureAtlas.GetData<Color>(imageData);

            Color[] imagePiece;
            Rectangle sourceRectangle;

            switch (blck)
            {
                case BlockType.Brick:
                    sourceRectangle = new Rectangle(0, 0, 16, 16);
                    break;
                case BlockType.Dirt:
                    sourceRectangle = new Rectangle(16, 0, 16, 16);
                    break;
                case BlockType.Gold:
                    sourceRectangle = new Rectangle(32, 0, 16, 16);
                    break;
                case BlockType.Grass:
                    sourceRectangle = new Rectangle(48, 0, 16, 16);
                    break;
                case BlockType.Gravel:
                    sourceRectangle = new Rectangle(0, 16, 16, 16);
                    break;
                case BlockType.Iron:
                    sourceRectangle = new Rectangle(80, 0, 16, 16);
                    break;
                case BlockType.Ladder:
                    sourceRectangle = new Rectangle(112, 16, 16, 16);
                    break;
                case BlockType.Lava:
                    sourceRectangle = new Rectangle(96, 0, 16, 16);
                    break;
                case BlockType.Leaves:
                    sourceRectangle = new Rectangle(112, 0, 16, 16);
                    break;
                case BlockType.None:
                    sourceRectangle = new Rectangle(16, 16, 16, 16);
                    break;
                case BlockType.Rock:
                    sourceRectangle = new Rectangle(16, 16, 16, 16);
                    break;
                case BlockType.Sand:
                    sourceRectangle = new Rectangle(32, 16, 16, 16);
                    break;
                case BlockType.Snow:
                    sourceRectangle = new Rectangle(48, 16, 16, 16);
                    break;
                case BlockType.Tree:
                    sourceRectangle = new Rectangle(80, 16, 16, 16);
                    break;
                case BlockType.Water:
                    sourceRectangle = new Rectangle(96, 16, 16, 16);
                    break;
                case BlockType.TNT:
                    sourceRectangle = new Rectangle(16, 32, 16, 16);
                    break;
                case BlockType.RedFlower:
                    sourceRectangle = new Rectangle(48, 32, 16, 16);
                    break;
                default:
                    sourceRectangle = new Rectangle(16, 16, 16, 16);
                    break;
            }
            imagePiece = GetImageData(imageData, _textureAtlas.Width, sourceRectangle);

            Texture2D subtexture = new Texture2D(Game.GraphicsDevice, sourceRectangle.Width, sourceRectangle.Height);
            subtexture.SetData<Color>(imagePiece);
            return subtexture;
        }

        public List<BlockType> Blocks = new List<BlockType>();

        Color[] GetImageData(Color[] colorData, int width, Rectangle rectangle)
        {
            Color[] color = new Color[rectangle.Width * rectangle.Height];
            for (int x = 0; x < rectangle.Width; x++)
                for (int y = 0; y < rectangle.Height; y++)
                    color[x + y * rectangle.Width] = colorData[x + rectangle.X + (y + rectangle.Y) * width];
            return color;
        }

        public int SelectedItemInd = 0;

        public override void Update(GameTime gameTime)
        {
            PlayerIndex controlIndex;
            MouseState mouseState = Mouse.GetState();
            int scrollWheelDelta = _previousMouseState.ScrollWheelValue - mouseState.ScrollWheelValue;
            //Debug.WriteLine(scrollWheelDelta);
            if (Game.InputState.IsButtonPressed(Buttons.LeftShoulder, Game.ActivePlayerIndex, out controlIndex)
                || scrollWheelDelta >= 120
                )
            {
                //_blockType--;
                //_blockType++;
                SelectedItemInd++;
                //Blocks[SelectedItemInd] = (BlockType)SelectedItemInd;
                //if (_blockType == BlockType.None) _blockType = BlockType.Water;
                if (SelectedItemInd == 13) SelectedItemInd = 0;
            }
            if (Game.InputState.IsButtonPressed(Buttons.RightShoulder, Game.ActivePlayerIndex, out controlIndex)
                || scrollWheelDelta <= -120
                )
            {
                //_blockType++;
                SelectedItemInd--;
                //if (_blockType == BlockType.MAXIMUM) _blockType = BlockType.Brick;
                if (SelectedItemInd == -1) SelectedItemInd = 12;
            }
            _previousMouseState = mouseState;
        }

        public override void Draw(GameTime gameTime)
        {
            //_spiteBatch.Draw(_tileStripTexture, new Vector2(_drawX, _drawY), Color.White);
            float selectorPos = ((int)SelectedItemInd/* - 1*/) * 32;


            _spiteBatch.Draw(GetSideTexture(Blocks[0]), new Rectangle(int.Parse("" + _drawX), int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[1]), new Rectangle(int.Parse("" + _drawX) + 32, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[2]), new Rectangle(int.Parse("" + _drawX) + 64, int.Parse("" + _drawY), 32, 32), Color.White);

            _spiteBatch.Draw(GetSideTexture(Blocks[3]), new Rectangle(int.Parse("" + _drawX) + 96, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[4]), new Rectangle(int.Parse("" + _drawX) + 128, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[5]), new Rectangle(int.Parse("" + _drawX) + 160, int.Parse("" + _drawY), 32, 32), Color.White);

            _spiteBatch.Draw(GetSideTexture(Blocks[6]), new Rectangle(int.Parse("" + _drawX) + 192, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[7]), new Rectangle(int.Parse("" + _drawX) + 224, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[8]), new Rectangle(int.Parse("" + _drawX) + 256, int.Parse("" + _drawY), 32, 32), Color.White);

            _spiteBatch.Draw(GetSideTexture(Blocks[9]), new Rectangle(int.Parse("" + _drawX) + 288, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[10]), new Rectangle(int.Parse("" + _drawX) + 320, int.Parse("" + _drawY), 32, 32), Color.White);
            _spiteBatch.Draw(GetSideTexture(Blocks[11]), new Rectangle(int.Parse("" + _drawX) + 352, int.Parse("" + _drawY), 32, 32), Color.White);

            _spiteBatch.Draw(GetSideTexture(Blocks[12]), new Rectangle(int.Parse("" + _drawX) + 384, int.Parse("" + _drawY), 32, 32), Color.White);



            _spiteBatch.Draw(_tileSelectorTexture, new Vector2(_drawX + selectorPos, _drawY), Color.White);


        }
    }
}
